package com.cy.detectsys.controller.azure;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cy.detectsys.entity.CaidaAsn;
import com.cy.detectsys.entity.DataCenterInfo;
import com.cy.detectsys.entity.IpNetCloudServiceInfo;
import com.cy.detectsys.entity.ProbeResultConnectAs;
import com.cy.detectsys.mapper.CaidaAsnMapper;
import com.cy.detectsys.mapper.DataCenterInfoMapper;
import com.cy.detectsys.mapper.IpNetCloudServiceInfoMapper;
import com.cy.detectsys.mapper.ProbeResultConnectAsMapper;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *  数据维护
 */
@RestController
public class DataMaintain {

    @Resource
    private DataCenterInfoMapper dataCenterInfoMapper;
    @Resource
    private IpNetCloudServiceInfoMapper ipNetCloudServiceInfoMapper;
    /**
     *     更新数据中心 承载云服务数量
     *     云服务数量:  ip_net_cloud_service_info  cloud_service字段去除得到
     */
    @RequestMapping("updateCloudServiceNumber")
    public void updateCloudServiceNumber() {
        for (DataCenterInfo dataCenterInfo : dataCenterInfoMapper.selectList(null)) {
            String dataCenterName = dataCenterInfo.getDataCenterName();

            QueryWrapper<IpNetCloudServiceInfo> ipNetCloudServiceInfoQueryWrapper = new QueryWrapper<>();
            ipNetCloudServiceInfoQueryWrapper.eq("data_center_name", dataCenterName);
            ipNetCloudServiceInfoQueryWrapper.select("distinct cloud_service");
            ipNetCloudServiceInfoQueryWrapper.isNotNull("cloud_service");

            PageHelper.startPage(1, 10);
            Long l = ipNetCloudServiceInfoMapper.selectCount(ipNetCloudServiceInfoQueryWrapper);

            dataCenterInfo.setCloudServerCount(l);
            dataCenterInfoMapper.updateById(dataCenterInfo);

        }

    }


    /**
     *     更新数据中心 ip规模
     *     ip规模: ip_scale
     */
    @RequestMapping("updateip_scaler")
    public void updateip_scaler() {
        for (DataCenterInfo dataCenterInfo : dataCenterInfoMapper.selectList(null)) {
            String dataCenterName = dataCenterInfo.getDataCenterName();
            QueryWrapper<IpNetCloudServiceInfo> ipNetCloudServiceInfoQueryWrapper = new QueryWrapper<>();
            ipNetCloudServiceInfoQueryWrapper.eq("data_center_name", dataCenterName);

            PageHelper.startPage(1, 10);
            Long l = ipNetCloudServiceInfoMapper.selectCount(ipNetCloudServiceInfoQueryWrapper);

            //拼凑格式
            HashMap<String, Long> count1 = new HashMap<>();
            count1.put("ipv4", l);
            List<Object> objects = new ArrayList<>();
            objects.add(count1);
            dataCenterInfo.setIpScale(JSON.toJSONString(objects));


            dataCenterInfoMapper.updateById(dataCenterInfo);



        }

    }


    @Resource
    private CaidaAsnMapper caidaAsnMapper;

    @Resource
    private ProbeResultConnectAsMapper probeResultConnectAsMapper;

    @RequestMapping("updateAsnIpCount")
    public void updateAsnIpCount() {

        for (CaidaAsn caidaAsn : caidaAsnMapper.selectList(null)) {

            Long asn = caidaAsn.getAsn();
            QueryWrapper<ProbeResultConnectAs> probeResultConnectAsQueryWrapper = new QueryWrapper<>();
            probeResultConnectAsQueryWrapper.eq("asn", asn);

            Long l = probeResultConnectAsMapper.selectCount(probeResultConnectAsQueryWrapper);

            caidaAsn.setIpCount(l);

            UpdateWrapper<CaidaAsn> objectUpdateWrapper = new UpdateWrapper<>();
            objectUpdateWrapper.eq("asn", asn);
            caidaAsnMapper.update(caidaAsn, objectUpdateWrapper);
        }

    }
}
